package com.shycloud.mido.organ.api.util;

import com.alibaba.excel.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * Baidu地图获取经纬度找到地址
 * @author yyy
 */
public class BaiduApi {
	/**
	 * 输入经纬度返回地址 key lng(经度),lat(纬度)
	 */
	public static String getPosition(String latitude, String longitude) throws MalformedURLException {
		String ak ="QZlzKPy7aHnsGlERQNDlkjp8sqEw3K0r";
		BufferedReader in = null;
		URL tirc = new URL("http://api.map.baidu.com/reverse_geocoding/v3/?ak="+ak+"&output=json&location=" +latitude+ "," +longitude);
		try {
			in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
			String res;
			StringBuilder sb = new StringBuilder("");
			while ((res = in.readLine()) != null) {
				sb.append(res.trim());
			}
			String str = sb.toString();
			System.out.println(str);
			if (!StringUtils.isEmpty(str)) {
				int lngStart = str.indexOf("city");
				int lngEnd = str.indexOf("city_level");
				if (lngStart > 0 && lngEnd > 0 ) {
					String ads = str.substring(lngStart+7,lngEnd-3);
					System.out.println("ads:" + ads);
					return ads;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
